<?php
namespace App\Http\Controllers\Admin;

use App\Models\Integral;
use App\Http\Resources\Admin\Integrals as IntegralCollection;
use Illuminate\Http\Request;

class IntegralsController extends Controller
{
    protected $_rules = [
        [
            'image' => 'string',
            'title' => 'required|string|max:20',
            'map_id' => 'required|string|max:255',
            'win_coin' => 'integer',
            'win_diamond' => 'integer',
            'entry_fee' => 'integer',
            'upgrade_need' => 'integer',
        ],
        // [
        //     'required' => ':attribute不能为空',
        //     'max' => ':attribute字符长度过长',
        //     'integer' => ':attribute必须为数字',
        //     'between' => ':attribute范围错误',
        // ],
        // [
        //     'link' => '跳转链接',
        //     'image' => '轮播图图片',
        //     'jump_type' => '跳转类型',
        //     'platform_type' => '客户端类型',
        // ],
    ];

    public function index()
    {
        $orderBy = $this->getOrderByQuery([
            'id',
            'win_coin',
            'win_diamond',
            'upgrade_need',
        ]);

        $result = Integral::orderByRaw($orderBy)
            ->orderByDesc('id')
            ->paginate(per_page());
        return new  IntegralCollection($result);
    }

    public function store(Request $request)
    {
        $inputs = $this->validate($request, $this->_rules[0]);

        $record = Integral::create($inputs);

        return $record;
    }

    public function update(Request $request, $id)
    {
        $record = Integral::findOrFail($id);

        $inputs = $this->validate($request, $this->allowNullRules($this->_rules[0]));
        $inputs = $this->unsetEmptyFields($inputs, ['image']);

        $record->update($inputs);
        return $record;
    }

    public function destroy($id)
    {
        $record = Integral::findOrFail($id);
        $result = Integral::destroy($id);
        return [];
    }
}
